<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>补偿案件管理</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/layui/css/layui.min.css">
    <link rel="stylesheet" href="/css/custom.form.css">
    <style>
        .layui-form-label {
            width: 100px;
        }

        .layui-input-block {
            margin-left: 130px;
        }

        .layui-input layui-unselect {
            cursor: pointer;
        }

        .layui-table {
            width: 96%;
        }

    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header"></div>
                <div class="layui-card-body">

                    <div class="layui-tab table_div" style="margin-left: 10px">
                        <div class="layui-tab-content">
                            <div class="layui-tab-item layui-show">
                                <form class="layui-form" id="searchForm">
                                    <div class="layui-input-inline">
                                        <label for="jcajbh1"></label>
                                        <input type="text" id="jcajbh1" class="layui-input" autocomplete="off"
                                               placeholder="请输入案件编号">
                                    </div>
                                    <div class="layui-input-inline">
                                        <label for="nsrsbh1"></label>
                                        <input type="text" id="nsrsbh1" class="layui-input" autocomplete="off"
                                               placeholder="请输入纳税人识别号">
                                    </div>
                                    <div class="layui-input-inline">
                                        <label for="nsrmc1"></label>
                                        <input type="text" id="nsrmc1" class="layui-input" autocomplete="off"
                                               placeholder="请输入纳税人名称">
                                    </div>
                                    <div class="layui-input-inline">
                                        <label for="jsks1"></label>
                                        <select id="jsks1" class="layui-input"></select>
                                    </div>
                                    <div class="layui-input-inline">
                                        <label for="ajztmc"></label>
                                        <select id="ajztmc" name="personSelect" lay-filter="personSelect">
                                            <option value="">请选择案件类型</option>
                                            <option value="沈抚案件">沈抚案件</option>
                                            <option value="组织收入">组织收入</option>
                                            <option value="一般申报">一般申报</option>
                                        </select>
                                    </div>
                                    <div class="layui-input-inline">
                                        <label for="sjzc"></label>
                                        <input type="text" id="sjzc" class="layui-input" autocomplete="off" placeholder="请输入主查">
                                    </div>
                                    <div class="layui-input-inline">
                                        <label for="sjxc"></label>
                                        <input type="text" id="sjxc" class="layui-input" autocomplete="off" placeholder="请输入协查">
                                    </div>
                                    <div class="layui-input-inline">
                                        <label for="startTime"></label>
                                        <input type="text" readonly class="layui-input" id="startTime" placeholder="立案时间起">
                                    </div>
                                    <div class="layui-input-inline">
                                        <label for="endTime"></label>
                                        <input type="text" readonly class="layui-input" id="endTime" placeholder="立案时间止">
                                    </div>
                                    <div class="layui-input-inline" style="margin-top: 10px">
                                        <button type="button" class="layui-btn" onclick="searchJckstz()" id="search">查询</button>
                                        <button type="button" class="layui-btn" id="resetBtn">重置</button>
                                    </div>
                                    <div class="layui-input-inline" style="margin-top: 10px">
                                        <button id="addBcaj" type="button" class="layui-btn">添加补偿案件</button>
                                    </div>
                                </form>
                                <!-- 查询列 -->
                                <table class="layui-table" id="grdbShowTable" lay-filter="grdbShowTable"></table>
                            </div>
                        </div>
                    </div>
                    <div id="debugtaxorgDiv" class="demo-transfer" style="display: none">
                        <div class="layui-form">
                            <!-- 主查人 -->
                            <div class="layui-form-item" style="margin-top: 5px">
                                <label class="layui-form-label">选择主查人</label>
                                <div class="layui-input-block">
                                    <select name="debugtaxorgSelect" lay-filter="debugtaxorgSelect" id="debugtaxorgSelect">
                                        <option value="">选择主查人</option>
                                    </select>
                                </div>
                            </div>
                            <!-- 协查人 -->
                            <div class="layui-form-item" style="margin-top: 40px">
                                <label class="layui-form-label">选择协查人</label>
                                <div class="layui-input-block">
                                    <select name="sjXcSelect" xm-select="xcrSelect" xm-select-skin="primary" lay-filter="sjXcSelect"
                                            id="sjXcSelect" multiple>
                                        <option value="">选择协查人</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!--添加案件-->
                    <form id="BcajForm" class="layui-form layui-row layui-col-space16" style="display: none">
                        <div class="layui-col-md6">
                            <div class="layui-form-item" style="margin-top: 20px">
                                <label class="layui-form-label">案件编号</label>
                                <div class="layui-input-block">
                                    <label>
                                        <input type="text" name="jcajbh" placeholder="请输入" class="layui-input">
                                    </label>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md6">
                            <div class="layui-form-item" style="margin-top: 20px">
                                <label class="layui-form-label">案件名称</label>
                                <div class="layui-input-block">
                                    <label>
                                        <input type="text" name="ajmc" placeholder="请输入" class="layui-input">
                                    </label>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md6">
                            <div class="layui-form-item" style="margin-top: 10px">
                                <label class="layui-form-label">纳税人识别号</label>
                                <div class="layui-input-block">
                                    <label>
                                        <input type="text" name="nsrsbh" placeholder="请输入" class="layui-input">
                                    </label>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md6">
                            <div class="layui-form-item" style="margin-top: 10px">
                                <label class="layui-form-label">纳税人名称</label>
                                <div class="layui-input-block">
                                    <label>
                                        <input type="text" name="nsrmc" placeholder="请输入" class="layui-input">
                                    </label>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md6">
                            <div class="layui-form-item">
                                <label class="layui-form-label">科室</label>
                                <div class="layui-input-block">
                                    <label for="jsks"></label><select id="jsks" name="jsks" class="layui-input"></select>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md6">
                            <div class="layui-form-item">
                                <label class="layui-form-label">案件类型:</label>
                                <div class="layui-input-block">
                                    <label>
                                        <select name="ajztmc" lay-filter="personSelect">
                                            <option value="">请选择案件类型</option>
                                            <option value="沈抚案件">沈抚案件</option>
                                            <option value="组织收入">组织收入</option>
                                            <option value="一般申报">一般申报</option>
                                        </select>
                                    </label>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md6">
                            <div class="layui-form-item" style="margin-top: 10px">
                                <label class="layui-form-label">主查</label>
                                <div class="layui-input-block">
                                    <label>
                                        <input type="text" name="sjzc" placeholder="请输入" class="layui-input">
                                    </label>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md6">
                            <div class="layui-form-item" style="margin-top: 10px">
                                <label class="layui-form-label">协查</label>
                                <div class="layui-input-block">
                                    <label>
                                        <input type="text" name="sjxc" placeholder="请输入" class="layui-input">
                                    </label>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md8">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">立案日期</label>
                                    <div class="layui-input-inline layui-input-wrap">
                                        <label for="larq"></label><input type="text" name="larq" id="larq" lay-verify="date"
                                                                         placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item" style="text-align: center">
                            <button class="layui-btn" lay-submit lay-filter="submitForm">确认</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="/layui/layui.all.js"></script>
<script src="/layui/formSelects-v3.js"></script>
<script src="/layui/xm-select.js"></script>
<script src="/js/core.util.js"></script>
<script th:inline="none">
    // 获取token
    var token = CoreUtil.getData("access_token");
    // 地址栏转义token中的#号
    var tokenQuery = token.replace("#", "%23");
    var grdbTableIns;
    var grybTableIns;
    var table = layui.table;
    var form = layui.form;
    var layer = layui.layer;
    var $ = jQuery = layui.jquery;
    var laydate = layui.laydate;
    var upload = layui.upload;
    var element = layui.element;
    var maxlength = 500;
    var debugtaxorg = "";
    var formSelects = layui.formSelects;
    layui.use(['table', 'layer', 'laydate', 'upload', 'element', 'form'], function () {
        // 默认初始化金三科室下拉选
        CoreUtil.sendPost("/workflow/activity/debugtaxorg", null, function (res) {
            if (res.code == '0') {
                let value = res.data; // 接口返回的数据
                let opt = "<option value=''>请选择科室</option>"; // 默认选项
                // 遍历接口返回的数据，生成选项
                for (let i in value) {
                    opt += '<option value="' + value[i].swjgjc +'">' + value[i].swjgjc + '</option>';
                }
                // 将生成的选项插入到 select 元素中
                $("#jsks1").html(opt);
                $("#jsks").html(opt);
                // 渲染 layui 的 select 组件
                form.render('select'); // 注意：必须调用渲染方法以更新 select 组件
            } else {
                // 如果接口返回错误
                layer.msg(res.msg, {icon: 2}); // 提示错误信息
                return false;
            }
        });
        //待调整检查科室表格
        grdbTableIns = table.render({
            elem: '#grdbShowTable',
            contentType: 'application/json',
            headers: {
                "authorization": token
            },
            page: true,    // 开启分页
            url: '/workflow/inspect/listdebugtaxorg',//数据接口
            method: 'POST',
            where: {
                lx: "7"
            },
            parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
                    "data": CoreUtil.isEmpty(res.data) ? null
                        : CoreUtil.paramsDecrypt(res.data.records)
                }
            },
            cols: [[
                {align: 'center', width: 230, field: 'jcajxxuuid', title: '检查UUID', sort: true, hide: true},
                {align: 'center', width: 220, field: 'jcajbh', title: '案件编号', sort: true},
                {align: 'center', width: 200, field: 'nsrsbh', title: '纳税人识别号', sort: true},
                {align: 'center', width: 200, field: 'nsrmc', title: '纳税人名称', sort: true},
                {align: 'center', width: 280, field: 'jsks', title: '科室', sort: true},
                {align: 'center', width: 280, field: 'ajztmc', title: '案件类型', sort: true},
                {align: 'center', width: 200, field: 'sjzc', title: '主查', sort: true},
                {align: 'center', width: 300, field: 'sjxc', title: '协查', sort: true},
                {
                    align: 'center', width: 150, field: 'larq', title: '立案时间', sort: true, templet: function (d) {
                        // 格式化日期为 'yyyy-MM-dd'
                        return layui.util.toDateString(d.larq, 'yyyy-MM-dd');
                    }
                },
                {fixed: 'right', align: 'center', width: 75, toolbar: "#grdbTool", title: '操作'}
            ]],
            toolbar: '#grdbToolbar'
        });
        //立案时间起
        laydate.render({
            elem: '#startTime'
            , type: 'date'
            , value: null
            , done: function (value) {
                if (value !== null && value !== undefined && value !== "") {
                    startTime = value;
                } else {
                    startTime = null;
                }
            }
        });
        //立案时间止
        laydate.render({
            elem: '#endTime'
            , type: 'date'
            , value: null
            , done: function (value) {
                if (value !== null && value !== undefined && value !== "") {
                    endTime = value;
                } else {
                    endTime = null;
                }
            }
        });
        //调整检查人
        table.on('tool(grdbShowTable)', function (obj) {
            debugtaxorg = "";
            CoreUtil.sendPost("/workflow/activity/querySelectJcr", null, function (res) {
                if (res.code == '0') {
                    let value = res.data;
                    let opt = "<option value = ''>请选择实际主查人</option>";
                    for (let i in value) {
                        opt += '<option value = "' + value[i].swryxm + '">' + value[i].swryxm + '</option>';
                    }
                    $("#debugtaxorgSelect").html(opt);
                    $("#sjXcSelect").html(opt);
                    form.render('select'); //需要渲染一下
                    formSelects.render('sjXcSelect'); //渲染多选框
                } else {
                    layer.msg(res.msg);
                    return false;
                }
            });
            // 弹窗逻辑
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "调整检察人员",
                area: ['700px', '420px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#debugtaxorgDiv"),
                btn: ['调整检查人员', '取消'],
                yes: function (index) {
                    // 获取两个下拉框的值
                    obj.data.selectedValue = $('#debugtaxorgSelect').val(); // 主查人下拉框值
                    // 获取协查人下拉框中所有选中的值
                    obj.data.xcSelectedValue = layui.formSelects.value('xcrSelect', 'val'); // 'xcrSelect' 是 xm-select 的值
                    console.log("协查人选中的值:", obj.data.xcSelectedValue);
                    console.log("主查人:", obj.data.selectedValue);
                    // 调用保存接口
                    CoreUtil.sendPost("/workflow/activity/saveZjcr", obj.data, function (res) {
                        if (res.code == '0') {
                            layer.msg("调整检查人员成功！");
                            searchJckstz(); // 重新加载数据
                        } else {
                            layer.msg(res.msg);
                        }
                    });
                    layer.close(index); // 关闭弹窗
                }
            });
        });
        // 监听重置按钮
        document.getElementById('resetBtn').addEventListener('click', function () {
            document.getElementById('searchForm').reset(); // 原生重置表单
            form.render(); // 重新渲染 Layui 的动态组件
        });

        document.getElementById('addBcaj').onclick = function () {
            layer.open({
                type: 1, // 类型为页面层
                title: '添加补偿案件', // 弹出框标题
                area: ['800px', '600px'], // 弹出框尺寸
                content: $('#BcajForm'), // 表单内容, // 弹出的内容
                maxmin: true,
                shadeClose: true, // 点击遮罩层关闭
            });
        };

        // 监听表单提交
        form.on('submit(submitForm)', function (data) {
            // 获取表单数据
            const formData = data.field;
            // 使用 AJAX 提交数据
            $.ajax({
                url: '/workflow/inspect/saveBcajgl', // 接口 URL
                type: 'POST',
                data: JSON.stringify(formData),// 发送表单数据
                headers: {
                    "authorization": token // 传递 token
                },
                contentType: 'application/json; charset=UTF-8',
                dataType: "json",
                success: function (response) {
                    // 服务器返回成功
                    if (response.data == true) {
                        layer.msg(response.msg, {icon: 1});
                        searchJckstz();
                        layer.closeAll(); // 关闭弹出层
                    } else {
                        layer.msg('表单提交失败：' + response.msg, {icon: 2});
                    }
                },
                error: function () {
                    // 发生错误
                    layer.msg('请求失败，请稍后重试', {icon: 2});
                }
            });

            // 阻止默认的表单提交事件
            return false;
        });
        // 渲染日期选择器
        laydate.render({
            elem: '#larq', // 绑定到的输入框
            format: 'yyyy-MM-dd', // 日期格式
            min: '1900-01-01', // 设置最小日期
            max: '2099-12-31', // 设置最大日期
            trigger: 'click', // 点击输入框时弹出日期选择器
            showBottom: false, // 隐藏底部按钮（选择确认的按钮）
            done: function (value, date, endDate) {
                // 用户选择日期后的回调函数
                console.log(value); // 打印选择的日期
            }
        });
    });

    //待调整检查科室查询
    function searchJckstz() {
        const lasjqValue = $("#startTime").val();
        const lasjzValue = $("#endTime").val();
        if (!lasjqValue && lasjzValue) {
            layer.msg('请输入立案时间起', {icon: 5, time: 1500});
            return;
        } else if (lasjqValue && !lasjzValue) {
            layer.msg('请输入立案时间止', {icon: 5, time: 1500});
            return;
        } else if (lasjqValue > lasjzValue) {
            layer.msg('立案时间起不能大于立案时间止', {icon: 5, time: 1500});
            return;
        }
        //这里以搜索为例
        grdbTableIns.reload({
            where: { //设定异步数据接口的额外参数，任意设
                jcajbh: $("#jcajbh1").val(),
                ajmc: $("#ajmc1").val(),
                nsrmc: $("#nsrmc1").val(),
                nsrsbh: $("#nsrsbh1").val(),
                ajztmc: $("#ajztmc").val(),
                jsks: $("#jsks1").val(),
                sjzc: $("#sjzc").val(),
                sjxc: $("#sjxc").val(),
                lasjq: lasjqValue,
                lasjz: lasjzValue
            }
            , page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    }
</script>

</body>
</html>